package com.jdone.compus.service;

import com.jdone.compus.model.BlowQuickDuration;
import com.jdone.compus.repository.BlowQuickDurationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class BlowQuickDurationService {

    @Autowired
    private BlowQuickDurationRepository repository;

    /** 新增快捷时长 */
    public BlowQuickDuration create(BlowQuickDuration dto) {
        return repository.save(dto);
    }

    /** 根据ID查询 */
    public BlowQuickDuration getById(Long id) {
        return repository.findById(id)
                .orElseThrow(() -> new RuntimeException("快捷时长选项未找到"));
    }

    /** 分页查询 */
    public Page<BlowQuickDuration> getAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /** 修改 */
    public BlowQuickDuration update(Long id, BlowQuickDuration dto) {
        BlowQuickDuration entity = getById(id);
        entity.setMinutes(dto.getMinutes());
        entity.setFee(dto.getFee());
        return repository.save(entity);
    }

    /** 删除 */
    public void delete(Long id) {
        BlowQuickDuration entity = getById(id);
        repository.delete(entity);
    }

    /**
     * 如果 deviceType 不为 null，则按类型过滤；否则查询所有
     */
    public Page<BlowQuickDuration> getAll(Integer deviceType, Pageable pageable) {
        if (deviceType != null) {
            return repository.findByDeviceType(deviceType, pageable);
        }
        return repository.findAll(pageable);
    }
}

